using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace MediaBackup
{
    public static class Log
    {
        #region Data
        private static string LogFileName;
        private static readonly string ModuleName;
        #endregion

        #region Methods
        static Log()
        {
            string modulePath;

            try
            {
                modulePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            }
            catch
            {
                modulePath = @"C:\MediaBackup.dll";
            }

            SetLogFile(modulePath + ".log");
            ModuleName = Path.GetFileName(modulePath);
        }

        public static void SetLogFile(string path)
        {
            LogFileName = path;
        }

        public static void Write(Exception e)
        {
            try
            {
                WriteLine(e.ToString());
                System.Diagnostics.EventLog.WriteEntry(ModuleName, e.ToString(), EventLogEntryType.Error);
            }
            catch
            {
            }
        }

        public static void WriteLine(string message)
        {
            try
            {
                message = SystemInformation.ComputerName + " @ " + DateTime.Now.ToString() + ": " + message;

                Console.WriteLine(message);

                StreamWriter writer = new StreamWriter(LogFileName, true);
                writer.WriteLine(message);
                writer.Close();
            }
            catch
            {
            }
        }
        #endregion
    }
}
